
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rule_text:'',
    rebate:0,
    bannerList: [],
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    tanIndex: "",
    noticeList: [],
    shareimg: "../../images/tb31.png",
    jiantou_img: "../../images/tb60.png",
    spec: "默认",
    shoppic: "../../images/shopimg.png",
    indexPic: "../../images/tb35.png",
    collectPic: "../../images/tb34.png",
    kefuPic: "../../images/tb33.png",
    groupList: [{
        img: "../../images/Component 53.png",
        num: "3",
        chanum: "1",
        endtime: "00:11:23"
      },
      {
        img: "../../images/Component 53.png",
        num: "3",
        chanum: "1",
        endtime: "00:11:23"
      },
    ],
    xing: "../../images/tb83.png",
    praise: "../../images/tb129.png",
    nopraise: "../../images/tb128.png",
    hide: true,
    commentList: [],
    show: false,
    show1: false,
    chooseGid: 0,
    price: 0,
    num: 1,
    comCount: 0,
    collection: 0,
    chooseMax: 0,
    time: '02:00',
    thumbUrl: getApp().globalData.thumbUrl,
    chooseCanyu: 0,
    uid: "",
    steps: [
      {
        text: '步骤一',
        desc: '描述信息',
        inactiveIcon: 'location-o',
        activeIcon: 'success',
      },
      {
        text: '步骤二',
        desc: '描述信息',
        inactiveIcon: 'like-o',
        activeIcon: 'plus',
      },
      {
        text: '步骤三',
        desc: '描述信息',
        inactiveIcon: 'star-o',
        activeIcon: 'cross',
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    var id = options.id;
    var name = options.name;
    var pid = options.uid;
    wx.setStorageSync('pid', pid)
    var uid = wx.getStorageSync('uid');
    wx.setNavigationBarTitle({
      title: name
    })
    var param = {
      id: id,
      uid: uid
    };
    app.wxRequest('POST', "Index/buyGoodsDetail", param, (res) => {
      var data = res.data;
      var rebate = data.rebate
      var rule_text = data.goodsInfo.rule_text
      var noticeList = data.noticeList;
      var goodsInfo = data.goodsInfo;
      var groupList = data.groupList;
      var attrList = data.attrList;
      var collection = data.collection;
      var ruleList = data.ruleList;
      if (collection == 1) {
        var collectPic = "../../images/tb04.png";
      } else if (collection == 0) {
        var collectPic = "../../images/tb05.png";
      }
      var content = that.escape2Html(goodsInfo.content);
      that.setData({
        rule_text:rule_text,
        rebate:rebate,
        noticeList: noticeList,
        goodsInfo: goodsInfo,
        groupList: groupList,
        attrList: attrList,
        spec: attrList[0].name,
        chooseGid: attrList[0].id,
        price: attrList[0].price,
        bannerList: goodsInfo.bannerArr,
        comCount: data.comCount,
        content: content.replace(/\<img/gi, '<img style="width:100%;height:auto" '),
        id: id,
        collectPic: collectPic,
        collection: collection,
        ruleList: ruleList,
        chooseMax: ruleList[0].id,
      })
      that.severalCountDown();
    }, (err) => {
      console.log(err.errMsg)
    })
    // that.severalCountDown();
    wx.hideShareMenu({
      success() {
        console.log("hideShareMenu")
      }
    })
  },
  onUnload: function () {
    var that = this;
    that.setData({
      clearTimer: false
    })
  },
  //时间显示小于10的前面补0方法
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },
  //倒计时函数
  severalCountDown: function () {
    var that = this;
    var time = 0;
    var obj = {};
    var groupList = that.data.groupList;
    if (!groupList) {
      return false;
    }
    //遍历数组，计算每个item的倒计时秒数
    groupList.forEach(function (item) {
      var endTime = new Date(item.endTime.replace(/-/g, "/")).getTime(); //结束时间时间戳
      var currentTime = new Date().getTime(); //当前时间时间戳
      time = (endTime - currentTime) / 1000;
      // 如果活动未结束
      if (time > 0) {
        var hou = parseInt(time / (60 * 60));
        var min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        var sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          hou: that.timeFormat(hou),
          min: that.timeFormat(min),
          sec: that.timeFormat(sec)
        }
      } else { //活动已结束
        obj = {
          hou: "00",
          min: "00",
          sec: "00"
        }
        clearTimeout(that.data.timeIntervalSeveral); //清除定时器
      }
      item.time = obj;
    })
    var timeIntervalSeveral = setTimeout(that.severalCountDown, 1000);
    that.setData({
      timeIntervalSeveral,
      groupList,
    })
  },
  chooseAttr: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var price = e.currentTarget.dataset.price;
    var name = e.currentTarget.dataset.name;
    var attrList = that.data.attrList;
    var chooseGid = that.data.chooseGid;
    var spec = that.data.spec;
    var price = that.data.price;
    for (var i in attrList) {
      if (attrList[i].id == id) {
        attrList[i].status = true;
        var chooseGid = attrList[i].id;
        var spec = attrList[i].name;
        var price = attrList[i].price;
      } else {
        attrList[i].status = false;
      }
    }
    that.setData({
      price: price,
      attrList: attrList,
      name: name,
      chooseGid: chooseGid,
      spec: spec,
      price: price
    })
    console.log(e)
    // var id=e.currentTarg
  },
  onClick: function (params) {
    var that = this;
    var uid = wx.getStorageSync('uid');
    if (!uid) {
      wx.navigateTo({
        url: '../authorization/authorization',
      })
      return false;
    }
    var chooseCanyu = that.data.chooseCanyu;
    var chooseGid = that.data.chooseGid;
    // var pid = that.data.pid;
    var num = that.data.num;
    if (chooseCanyu > 0) {
      if (chooseGid) {
        wx.navigateTo({
          url: '../joinConfirm/joinConfirm?num=' + num + "&gid=" + chooseGid + "&joinId=" + chooseCanyu,
        })
      } else {
        that.showFrame("请选择商品规格");
      }
    } else if (chooseCanyu == -1) {
      if (chooseGid) {
        console.log(chooseCanyu)
        console.log(chooseGid)
        wx.navigateTo({
          url: '../buyGroupComfirm/buyGroupComfirm?gid='+chooseGid+"&num="+num,
        })
      } else {
        that.showFrame("请选择商品规格");
      }

    } else {
      that.setData({
        show: false
      })
    }

  },
  returnIndex: function () {
    wx.switchTab({
      url: '../index/index',
    })
  },
  escape2Html(str) {
    var arrEntities = {
      'lt': '<',
      'gt': '>',
      'nbsp': ' ',
      'amp': '&',
      'quot': '"'
    };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
      return arrEntities[t];
    }).replace('<section', '<div').replace('<img', '<img style="width:100%;height:auto" ');
  },
  showMoreComment: function (e) {
    var that = this;
    var id = that.data.id;
    var uid = wx.getStorageSync('uid');
    if (!uid) {
      wx.navigateTo({
        url: '../authorization/authorization',
      })
    } else {
      wx.navigateTo({
        url: '../comment/comment?id=' + id + "&type=2",
      })
    }
  },
  collect: function () {
    var that = this;
    var collection = that.data.collection;
    if (collection == 1) {
      collection = 0;
      var collectPic = "../../images/tb05.png";
    } else if (collection == 0) {
      collection = 1;
      var collectPic = "../../images/tb04.png";
    }
    var uid = wx.getStorageSync('uid');
    if (!uid) {
      wx.navigateTo({
        url: '../authorization/authorization',
      })
      return false;
    }
    var id = that.data.id;
    var param = {
      uid: uid,
      tid: id,
      type: 2
    }
    app.wxRequest('POST', "Index/collGoods", param, (res) => {

    }, (err) => {
      console.log(err.errMsg)
    })
    that.setData({
      collection: collection,
      collectPic: collectPic
    })
  },
  chooseRule: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      chooseMax: id
    })
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  joinGroup: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      chooseCanyu: id,
      show: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var uid = wx.getStorageSync('uid')
    that.setData({
      uid: uid
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    var goodsInfo = that.data.goodsInfo;
    var uid = wx.getStorageSync("uid");
    var id = that.data.id;
    return {
      title: goodsInfo.name,
      imageUrl: that.data.thumbUrl + goodsInfo.picture,
      path: '/pages/shopDetails/shopDetails?id=' + id + "&name=" + goodsInfo.name + "&uid=" + uid
    }
  },
  toestimate() {
    wx.navigateTo({
      url: '/pages/estimate/estimate',
    })
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onClose1() {
    this.setData({
      show1: false
    });
  },
  showPopup() {
    this.setData({
      show: true,
      chooseCanyu: 0
    });
  },
  showPopup1() {
    this.setData({
      show1: true,
      chooseCanyu: 0
    });
  },
  onChange(event) {
    var that = this;
    var num = event.detail;
    that.setData({
      num: num
    })
    console.log(event.detail);
  },
  topay() {
    var that = this;
    var chooseGid = that.data.chooseGid;
    var num = that.data.num;
    var chooseMax = that.data.chooseMax;
    var uid = wx.getStorageSync('uid');
    if (uid) {
      wx.navigateTo({
        url: '/pages/pay/pay?gid=' + chooseGid + "&num=" + num + "&ruleId=" + chooseMax,
      })
    } else {
      wx.navigateTo({
        url: '../authorization/authorization',
      })
    }
  },
  //立即购买
  showBuyBtn: function (e) {
    var that = this;
    var uid = wx.getStorageSync('uid');
    if (!uid) {
      wx.navigateTo({
        url: '../authorization/authorization',
      })
      return false;
    } else {
      that.setData({
        chooseCanyu: -1,
        show: true
      })
    }

  },
  showTiShi: function () {
    wx.navigateTo({
      url: '../authorization/authorization',
    })
  },
  // bindCurrent:function(e){
  //   console.log(e)
  // },
  showFrame: function (text) {
    wx.showToast({
      title: text,
      icon: "none",
      duration: 1500
    })
  }
})